﻿using System;
using System.Web.Mvc;

namespace RTW.OA.Attributes
{
    public class CheckLoginAttribute : ActionFilterAttribute
    {
        public CheckLoginAttribute(bool isChildPage = false, bool isCheck = true)
        {
            this.IsChildPage = isChildPage;
            this.IsCheck = isCheck;
        }

        public bool IsChildPage { get; set; }

        public bool IsCheck { get; set; } = true;

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var httpContext = filterContext.HttpContext;
            if (this.IsCheck && httpContext.Session["UserInfo"] == null)
            {
                if (this.IsChildPage)
                {
                    httpContext.Response.Write("<script languge='text/javascript'>top.location.href='/login';</script>");
                    httpContext.Response.End();
                }
                else
                {
                    filterContext.Result = new RedirectResult("/Login");
                }
            }
            base.OnActionExecuting(filterContext);
        }
    }
}